package com.th.supcom.pds.impl.service.internal;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.carefx.framework.exception.BusinessException;
import net.carefx.framework.translate.TranslatorManager;

import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.th.supcom.pds.domain.pojo.PhamPropertyTable;
import com.th.supcom.pds.domain.vo.DrugInfoVO;
import com.th.supcom.pds.domain.vo.DrugSearchVO;
import com.th.supcom.pds.impl.manager.PhamPropertyTableManager;
import com.th.supcom.pds.impl.manager.impl.PhamPropertyTableManagerImpl;
import com.th.supcom.pds.internal.api.DrugMaintainService;

public class DrugMaintainServiceImpl implements DrugMaintainService
{

    private PhamPropertyTableManager m_phamPropertyTableManager;

    private TranslatorManager m_translatorManager;

    /**
     * 默认构造器
     */
    public DrugMaintainServiceImpl ()
    {

    }

    @Override
    public List <DrugInfoVO> getDrugBasicInfo (DrugSearchVO bsinfoIn, final int offeset, final int limit)
    {
        DrugMaintainServiceImpl.s_logger.debug ("进入getDrugBasicInfo方法,入参:bsinfoIn:{}",new Object[]{bsinfoIn});
        DrugSearchVO bsinfo = new DrugSearchVO ();
        if (bsinfoIn == null)
        {
            DrugMaintainServiceImpl.s_logger.warn ("传入参数为空");
        }
        else
        {
            bsinfo = bsinfoIn;
        }

        final Map <String, Object> params = new HashMap <String, Object> ();
        params.put ("expensive", bsinfo.getExpensive ());
        params.put ("phamCateCode", bsinfo.getPhamCateCode ());
        params.put ("restrictionSign", bsinfo.getRestrictionSign ());
        params.put ("phamName", bsinfo.getPhamName ());
        params.put ("svrModeTypeId", bsinfo.getSvrModeTypeId ());
        params.put ("putDrugMode", bsinfo.getPutDrugMode ());
        params.put ("supplyIndicator", bsinfo.getSupplyIndicator ());
        params.put ("orgId", bsinfo.getOrgId ());
        List <DrugInfoVO> basicInfoList = null;
        try
        {
            basicInfoList = m_phamPropertyTableManager.findPhamPropertyTableListWithPageByMap (params, offeset, limit);
            basicInfoList = m_translatorManager.translate (basicInfoList);
        }
        catch (final Exception e)
        {
            DrugMaintainServiceImpl.s_logger.error (e.getMessage ());
            throw new BusinessException ("数据库查询出现异常" + e.getMessage (), e);
        }
       
        return basicInfoList;
    }

    @Override
    public List <DrugInfoVO> getDrugBasicInfoByName (final String name, String orgId, final int offeset, final int limit)
    {
        DrugMaintainServiceImpl.s_logger.debug ("进入getDrugBasicInfoByName方法,入参:name:{},orgId:{}",new Object[]{name,orgId});
        final Map <String, Object> params = new HashMap <String, Object> ();
        params.put ("phamName", name);
        params.put ("orgId", orgId);
        List <DrugInfoVO> basicInfoList = null;
        try
        {
            basicInfoList = m_phamPropertyTableManager.findPhamPropertyTableListWithPageByName (new RowBounds (offeset, limit), params);
            basicInfoList = m_translatorManager.translate (basicInfoList);

        }
        catch (final Exception e)
        {
            DrugMaintainServiceImpl.s_logger.error (e.getMessage ());
            throw new BusinessException ("数据库查询出现异常" + e.getMessage (), e);
        }

        return basicInfoList;

    }

    @Override
    public int getDrugBasicInfoCount (final DrugSearchVO bsinfo)
    {
        DrugMaintainServiceImpl.s_logger.debug ("进入getDrugBasicInfoCount方法,入参:bsinfo:{}",new Object[]{bsinfo});
        final Map <String, Object> params = new HashMap <String, Object> ();
        params.put ("expensive", bsinfo.getExpensive ());
        params.put ("phamCateCode", bsinfo.getPhamCateCode ());
        params.put ("restrictionSign", bsinfo.getRestrictionSign ());
        params.put ("phamName", bsinfo.getPhamName ());
        params.put ("svrModeTypeId", bsinfo.getSvrModeTypeId ());
        params.put ("putDrugMode", bsinfo.getPutDrugMode ());
        params.put ("supplyIndicator", bsinfo.getSupplyIndicator ());
        params.put ("orgId", bsinfo.getOrgId ());
        int count = 0;
        try
        {
            count = m_phamPropertyTableManager.findPhamPropertyTableCountByMap (params);
        }
        catch (final Exception e)
        {
            DrugMaintainServiceImpl.s_logger.error (e.getMessage ());
            throw new BusinessException ("查询失败:" + e.getMessage (), e);
        }
        return count;

    }

    @Override
    public int getDrugBasicInfoCountByName (final String name, String deptCode)
    {
        DrugMaintainServiceImpl.s_logger.debug ("进入getDrugBasicInfoCountByName方法,入参:name:{},deptCode:{}",new Object[]{name,deptCode});
        final Map <String, Object> params = new HashMap <String, Object> ();
        params.put ("phamName", name);
        params.put ("orgId", deptCode);
        int count = 0;
        try
        {
            count = m_phamPropertyTableManager.findPhamPropertyTableCountByName (params);
        }
        catch (final Exception e)
        {
            DrugMaintainServiceImpl.s_logger.error (e.getMessage ());
            throw new BusinessException ("查询失败:" + e.getMessage (), e);
        }
        return count;

    }

    @Override
    public void modifyDrugProperty (final List <PhamPropertyTable> phamPropertyTableList, String deptCode)
    {

        DrugMaintainServiceImpl.s_logger.debug ("进入modifyDrugProperty方法,入参:phamPropertyTableList:{},deptCode:{}",new Object[]{phamPropertyTableList,deptCode});
        if (phamPropertyTableList == null)
        {
            return;
        }
        if (deptCode == null)
        {
            throw new BusinessException ("没找到对应科室");
        }
        for (final PhamPropertyTable phamPropertyTable : phamPropertyTableList)
        {
            // 如果药品编号在属性表中，执行更新操作，否则执行插入操作
            final List <PhamPropertyTable> list = m_phamPropertyTableManager.findPhamPropertyTableListByExample (phamPropertyTable);
            try
            {
                if (list.isEmpty ())
                {
                    final Long id = m_phamPropertyTableManager.getPhamPropertyTableSeq ();
                    phamPropertyTable.setPhamPropertySerialNo (id);// 序列号，药品编码
                    phamPropertyTable.setOrgId (deptCode);// 科室
                    setDefaultValue (phamPropertyTable);
                    m_phamPropertyTableManager.addPhamPropertyTable (phamPropertyTable);
                    m_phamPropertyTableManager.addPhamAllocationTable (id);
                }
                else
                {
                    m_phamPropertyTableManager.modifyPhamPropertyTableByExample (phamPropertyTable);
                }
            }
            catch (final Exception e)
            {
                DrugMaintainServiceImpl.s_logger.error (e.getMessage ());
                throw new BusinessException ("保存失败" + e.getMessage (), e);
            }
        }

    }

    private void setDefaultValue (final PhamPropertyTable phamPropertyTable)
    {
        if (phamPropertyTable.getSupplyIndicator () == null)
        {
            phamPropertyTable.setSupplyIndicator ("1");
        }
        if (phamPropertyTable.getExpensive () == null)
        {
            phamPropertyTable.setExpensive ("0");
        }
        if (phamPropertyTable.getSvrModeTypeId () == null)
        {
            phamPropertyTable.setSvrModeTypeId ("1");
        }
        if (phamPropertyTable.getPutDrugMode () == null)
        {
            phamPropertyTable.setPutDrugMode ("1");
        }
    }

    public void setPhamPropertyTableManager (final PhamPropertyTableManager phamPropertyTableManager)
    {

        m_phamPropertyTableManager = phamPropertyTableManager;
    }

    public void setTranslatorManager (final TranslatorManager translatorManager)
    {

        m_translatorManager = translatorManager;
    }

    /** 公司版权信息. */
    public static final String copyright = "Copyright 2014, TH Supcom Corporation";
    /** 日志记录器. */
    private static final Logger s_logger = LoggerFactory.getLogger (PhamPropertyTableManagerImpl.class);
    /** 提交路径. */
    public static final String SCC_BRANCH = "$URL$";

    /** 提交日期. */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";

    /** 最终修改人员. */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";
    /** 最终版本号. */
    public static final String SCC_REVISION = "$Revision$";

    @Override
    public List <DrugInfoVO> searchDrugBasicInfoByName (String name, String orgId, int offeset, int limit)
    {
        s_logger.debug ("进入searchDrugBasicInfoByName方法,入参:name:{},orgId:{}",new Object[]{name,orgId});
        final Map <String, Object> params = new HashMap <String, Object> ();
        params.put ("phamName", name);
        params.put ("orgId", orgId);
        List <DrugInfoVO> basicInfoList = null;
        try
        {
            basicInfoList = m_phamPropertyTableManager.searchPhamPropertyTableListWithPageByName (new RowBounds (offeset, limit), params);
            basicInfoList = m_translatorManager.translate (basicInfoList);

        }
        catch (final Exception e)
        {
            DrugMaintainServiceImpl.s_logger.error (e.getMessage ());
            throw new BusinessException ("数据库查询出现异常" + e.getMessage (), e);
        }
        return basicInfoList;

    }

    @Override
    public int searchDrugBasicInfoCountByName (String name, String deptCode)
    {
        s_logger.debug ("进入searchDrugBasicInfoCountByName方法,入参:name:{},deptCode:{}",new Object[]{name,deptCode});
        final Map <String, Object> params = new HashMap <String, Object> ();
        params.put ("phamName", name);
        params.put ("orgId", deptCode);
        int count = 0;
        try
        {
            count = m_phamPropertyTableManager.searchPhamPropertyTableCountByName (params);
        }
        catch (final Exception e)
        {
            DrugMaintainServiceImpl.s_logger.error (e.getMessage ());
            throw new BusinessException ("查询失败:" + e.getMessage (), e);
        }
        return count;

    }
}
